# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=elasticsearch
pkgver=6.4.3
pkgrel=0
pkgdesc="Open Source, Distributed, RESTful Search Engine"
url="https://www.elastic.co/products/elasticsearch"
arch="aarch64 x86 x86_64 ppc64le"
license="Apache-2.0"
depends="java-jna-native>=4.1 openjdk8-jre bash"
makedepends=""
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="$pkgname-doc $pkgname-x-pack:_x_pack"
options="!check" # no test suite
pkgusers="elastico"
pkggroups="elastico"
source="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$pkgver.tar.gz
	default-config.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	README.alpine
	busybox-mktemp.patch
	"
builddir="$srcdir/$pkgname-$pkgver"

_default_module=transport-netty4
_modules="
	aggs-matrix-stats
	analysis-common
	ingest-common
	lang-expression
	lang-mustache
	lang-painless
	mapper-extras
	parent-join
	percolator
	rank-eval
	reindex
	repository-url
	tribe
	"

for _mod in $_modules; do
	subpackages="$subpackages $pkgname-$_mod:_${_mod//-/_}"
	eval "_${_mod//-/_}() { _builtin_module $_mod; }"
done

_basedir="/usr/share/java/$pkgname"

package() {
	local destdir="$pkgdir/$_basedir"
	local confdir="$pkgdir/etc/$pkgname"
	local docdir="$pkgdir/usr/share/doc/elasticsearch"
	local heapsize="256m"

	cd "$builddir"
	install -dm755 "$destdir"/lib "$destdir"/modules "$destdir"/bin
	cp -R lib/* "$destdir"/lib

	install -dm755 "$docdir"
	install -dm755 -o elastico -g elastico "$confdir"
	install -m644 -t "$confdir" config/*

	# remove windows files
	find bin -type f -name *.bat -exec rm -f {} \;
	find bin -type f -name *.exe -exec rm -f {} \;

	# ES bin script parses the new jvm.options file
	cp -R bin/* "$destdir"/bin

	# ES does not run without a transport module
	install -dm755 "$destdir/modules/$_default_module"
	install -m644 -t "$destdir/modules/$_default_module" modules/"$_default_module"/*

	# reduce heap sizes
	sed -i \
		-e "s|^-Xms.*|-Xms$heapsize|" \
		-e "s|^-Xmx.*|-Xmx$heapsize|" \
		$confdir/jvm.options

	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname

	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname

	install -m644 -D "$srcdir"/$pkgname.logrotated \
		"$pkgdir"/etc/logrotate.d/$pkgname

	install -m644 -D "$srcdir"/README.alpine \
		"$docdir"

	# Enforce plugins directory relocation
	# Passing the "path.plugins" or "es.default.path.plugins" arguments
	# at runtime does not seem to work in v6.4,
	# it generates an exception if "$destdir"/plugins does not exist.
	# The plugins directory will be shared among all ES instances.
	ln -s /var/lib/elasticsearch/_default/plugins "$destdir"/plugins
}

_builtin_module() {
	local name="$1"
	local destdir="$subpkgdir/$_basedir/modules/$name"

	install -dm755 "$destdir"
	install -m644 -t "$destdir" "$builddir"/modules/$name/*
}

_x_pack() {
	install -dm755 "$subpkgdir"/$_basedir/modules
	mv "$builddir"/modules/x-pack* "$subpkgdir"/$_basedir/modules

	# remove module compiled against GLIBC
	rm -rf "$subpkgdir"/$_basedir/modules/x-pack-ml
}

sha512sums="2f48720fd1644b1d0f103ca93bd19632c2a12bd3c94434ddf58bc692cb0b23631222f8d23cc0d0e85e271e11099fe0fc8aedf402906b76845ad680eb592f8987  elasticsearch-6.4.3.tar.gz
dffe445d62e45bcec5ccffc8e140e19ac35613c008a956481521d0b6b74a8899f5a8852c6b45c58fa1c6d8f80466491d9a7b8bdea40c2a2614ba8b46889fd1ac  default-config.patch
24d17bdfad1d4e53a3568c77e14a712ab0cd17144eb95b2bdf659466dbe5cf48d1ac48285c644c0bf1dc7f309012ccb99956fcd5134e6ac85651c94bee8b5add  elasticsearch.initd
2ab1baf1b5c8782f3f371ba221aadd3e841abc62175f0b1ddcfc68d711e2370465124e076f8cc2e549c25a1da9db8c90172b2f410bd6bbe4153f0e831620b6ba  elasticsearch.confd
450879a8ab49c4debac3f8ed484918d48428cabf300b8196db785930a9bc56ce8a153b8d4c91655cfbe9ff50bb978b3c0f87eef99ed36213463f206b68d06590  elasticsearch.logrotated
6de81485cdc059afef58382862e4155482463fde0b604aaa8dbe904c778b841467c4a383a5e54acd09e3436f1fb7be9923e001fb77bd3d7894e113a5e0f4036b  README.alpine
1f9d13bbc42c04c523ef4e9ce734f919ec3d79748d78f288ed3dd725cf7c74e83d66d8b48fbc782b27b1a760096a647cb33773578071710542b19fda18ea0b84  busybox-mktemp.patch"
